package com.ruoyi.traplight.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.traplight.domain.PhotoDao;
import com.ruoyi.traplight.domain.SelectPhotoDao;
import com.ruoyi.traplight.domain.TagAndCorner;
import com.ruoyi.traplight.mapper.TrapLightDataMapper;
import com.ruoyi.traplight.service.PhotoService;
import com.ruoyi.utils.ParseString;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api("测报灯图片类")
@Slf4j
@RestController
@RequestMapping("/photo")
public class PhotoController extends BaseController {

    @Value("${ruoyi.profile}")
    private String profile;
    @Autowired
    private PhotoService photoService;
    @Autowired
    private TrapLightDataMapper trapLightDataMapper;

    @GetMapping("/deletePhotoById")
    public AjaxResult deletePhotoById(int id){
        Integer counts = photoService.deletePhoto(id);
        if(counts == 0){
            return AjaxResult.success("该信息在数据库中不存在，请刷新或联系管理员");
        }
        return AjaxResult.success("删除成功，请刷新");
    }
    @ApiOperation("根据设备名下载图片")
    @GetMapping("/imageDownloadByDevice")
    public AjaxResult imageDownloadByDevice(SelectPhotoDao selectPhoto){
        List<PhotoDao> photoDaos = trapLightDataMapper.selectPhotoList(selectPhoto);
        List<String> photoPathList = new ArrayList<>();
        for(PhotoDao photoDao : photoDaos){
            String photoPath = photoDao.getPhotoPath();
            photoPathList.add(photoPath);
        }
        return AjaxResult.success(photoPathList);

    }
    @ApiOperation("前端画框更新数据库值")
    @PostMapping("/getCornerAndTag")
    public AjaxResult getCornerAndTag(@RequestBody TagAndCorner tagAndCorner){
//        Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
//        Integer userId=Integer.valueOf(userIdL.intValue());
        int id = tagAndCorner.getId();
        if(tagAndCorner.getCorner().equals("[]") || tagAndCorner.getTagForDraw().equals("[]")) {
            SelectPhotoDao newPhoto = new SelectPhotoDao().setId(id).setCorner("").setTagForDraw("").setTagManual("");
            trapLightDataMapper.updatePhotoInfoById(newPhoto);
        }else {
            String corner = tagAndCorner.getCorner();
//        [\"y1\",\"y1\"]//解析成["y1","y1"]格式
            String tagForDraw = tagAndCorner.getTagForDraw();
//        移除字符串中的转义符号\
            String cleanedString = tagForDraw.replace("\\", "");
//        移除首尾的[ 和 ]
            tagForDraw = cleanedString.substring(1, cleanedString.length() - 1);
            // 将字符串以逗号分隔成多个子字符串
            String[] array = tagForDraw.split(",");

//        // 移除子字符串首尾的双引号 "
//        for (int i = 0; i < array.length; i++) {
//            array[i] = array[i].replaceAll("\"", "").trim();
//        }

            String tagForDrawRes = Arrays.toString(array);
            String tagManual = ParseString.getTagNum(tagForDrawRes);
            SelectPhotoDao newPhoto = new SelectPhotoDao().setId(id).setCorner(corner).setTagForDraw(tagForDrawRes).setTagManual(tagManual);
            trapLightDataMapper.updatePhotoInfoById(newPhoto);
        }
        return AjaxResult.success("更新虫情信息成功");
    }
}
